package org.ns.tomdog.startup;

import org.ns.tomdog.classloader.ClassPath;
import org.ns.tomdog.classloader.CommonClassLoader;
import org.ns.tomdog.config.ServerConfig;
import org.ns.tomdog.server.Server;

import java.io.File;

public class Bootstrap {

    private String basePath;
    private CommonClassLoader commonClassLoader;
    private ServerConfig serverConfig;
    private Server server;
    // 入口方法
    public static void main(String[] args) throws Exception {
        Bootstrap bootstrap = new Bootstrap();
        bootstrap.init();  // 初始化
        bootstrap.start(); // 启动
    }
    void init() {
        try {
            basePath = System.getProperty("user.dir");
            ClassPath classPath = new ClassPath(null, new File(basePath, "lib"));
            File servetConfigPath = new File(basePath, "conf/server.xml");
            commonClassLoader = new CommonClassLoader(classPath);
            serverConfig = new ServerConfig(basePath, servetConfigPath, commonClassLoader);
            serverConfig.parse(); // server.xml 解析
            server = serverConfig.getServerStartup();
            server.init(); // 服务初始化
        } catch (Exception e) {
            e.printStackTrace();
            System.exit(1);
        }
    }
    void start() {
        try {
            // 服务启动
            server.start();
        } catch (Exception e) {
            e.printStackTrace();
            System.exit(1);
        }
    }

}
